var DiscountToExamineTable = {
    id: "discountToExamineTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}

DiscountToExamineTable.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: '', field: 'discountGrade', visible: false, align: 'center', valign: 'middle'},
        {title: '门店', field: 'outPatientName', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '申请折扣', field: 'discount', align: 'center', valign: 'middle', sortable: true, width: 120},
        {title: '申请人', field: 'applyUser', align: 'center', valign: 'middle', sortable: true, width: 100},
        {
            title: '开单时间', field: 'operationTime', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {
            title: '审核状态', field: 'status', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (value == 0) {
                    return "待审核";
                } else if (value == 1) {
                    if (row.discountGrade == 1) {
                        return "审核完成"
                    } else {
                        return "一级审批结束"
                    }
                } else {
                    return "审核完成"
                }
            }
        },
        {
            title: '审核结果', field: 'auditStatus', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (value == 0) {
                    return "未同意";
                } else if (value == 1) {
                    return "待审核";
                } else {
                    return "已同意";
                }
            }
        },
        {title: '审核人1', field: 'auditUser1', align: 'center', valign: 'middle', sortable: true, width: 120},
        {
            title: '审核时间1', field: 'auditTime1', align: 'center', valign: 'middle', sortable: true, width: 170,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '审核人2', field: 'auditUser2', align: 'center', valign: 'middle', sortable: true, width: 120},
        {
            title: '审核时间2', field: 'auditTime2', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '备注', field: 'remark', align: 'center', valign: 'middle', sortable: true, width: 120},
        {
            title: '操作', field: 'id', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                if (document.getElementById("auditStatusValue").value == 1) {
                    return '<span onclick="DiscountToExamineTable.modifys(' + "'" + row.id + "'," + "'" +
                        row.reservationsId + "'," + "'" + row.customerId + "'," + "'" + row.operationTime + "'," + "'" + row.outPatientName + "'," + "'" + row.status + "'," + row.discount + ')" ' +
                        'style="font-weight: bold;cursor: pointer">【详情】</span>'
                } else {
                    return '<span onclick="DiscountToExamineTable.modify(' + "'" + row.id + "'," + "'" +
                        row.reservationsId + "'," + "'" + row.customerId + "'," + "'" + row.operationTime + "'," + "'" + row.outPatientName + "'," + "'" + row.status + "'," + row.discount + ')" ' +
                        'style="font-weight: bold;cursor: pointer">【折扣审核】</span>'
                }
            }
        }
    ];
};

DiscountToExamineTable.modify = function (id, reservationsId, customerId, operationTime, outPatientName, status, discount) {
    if (status == 2) {
        layer.msg('该记录已审核完成！', {time: 800, icon: 2});
        return false;
    } else {
        var index = layer.open({
            type: 2,
            title: '折扣审核',
            area: ['100%', '100%'],
            fix: true,
            maxmin: true,
            content: Feng.ctxPath + '/discountAudit/discountAudit_toExamine/' + id + '/' + reservationsId + '/' + customerId + '/' + operationTime + '/' + outPatientName + '/' + status + '/' + discount
        });
        this.layerIndex = index;
    }
};

DiscountToExamineTable.modifys = function (id, reservationsId, customerId, operationTime, outPatientName, status, discount) {
    var index = layer.open({
        type: 2,
        title: '详情',
        area: ['100%', '100%'],
        fix: true,
        maxmin: true,
        content: Feng.ctxPath + '/discountAudit/discountAudit_toExamines/' + id + '/' + reservationsId + '/' + customerId + '/' + operationTime + '/' + outPatientName + '/' + status + '/' + discount
    });
    this.layerIndex = index;
};

DiscountToExamineTable.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#marketInputPublic", parent.document).val();  //姓名   电话   微信  预约号
    queryData['outPatientName'] = $("#outPatientName").val();
    queryData['auditStatus'] = $("#auditStatus").val();
    queryData['startCreateTime'] = $("#startCreateTime").val();
    queryData['endCreateTime'] = $("#endCreateTime").val();
    queryData['auditStatusValue'] = $("#auditStatusValue").val();
    queryData['auditNode'] = $("#auditNode").val();
    return queryData;
}

$("#marketSearchAll", parent.document).click(function () { // 主页搜索
    DiscountToExamineTable.search();
});

DiscountToExamineTable.resetSearch = function () {
    $("#marketInputPublic", parent.document).val("");
    var auditNode = $("#auditNode").val();
    if (auditNode == "0") {
        $("#auditStatus").val("");
        $("#startCreateTime").val("");
        $("#endCreateTime").val("");
    } else {
        $("#outPatientName").val("");
        $("#auditStatus").val("");
        $("#startCreateTime").val("");
        $("#endCreateTime").val("");
    }
    DiscountToExamineTable.search();
};

DiscountToExamineTable.search = function () {
    DiscountToExamineTable.table.refresh({query: DiscountToExamineTable.formParams()});
    $.ajax({
        url: Feng.ctxPath + "/discountAudit/getCount",
        type: "post",
        data: {
            "outPatientName": $("#outPatientName").val(),
            "auditStatus": $("#auditStatus").val(),
            "startCreateTime": $("#startCreateTime").val(),
            "endCreateTime": $("#endCreateTime").val(),
            "auditStatusValue": $("#auditStatusValue").val(),
            "reach": $("#marketInputPublic", parent.document).val(),
            "auditNode": $("#auditNode").val()
        },
        success: function (data) {
            $("#pendingApproval").html(data[0].countNum);
            $("#approved").html(data[1].countNum);
        }
    });
};

$(function () {
    if (document.getElementById("deptName").value.indexOf("总公司") != -1) {$("#deptName").show();} else {$("#deptName").hide();}
    document.getElementById("startCreateTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endCreateTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    var defaultColunms = DiscountToExamineTable.initColumn();
    var table = new BSTable(DiscountToExamineTable.id, "/discountAudit/discountAuditList", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(DiscountToExamineTable.formParams());
    DiscountToExamineTable.table = table.init();
    DiscountToExamineTable.search();
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function () {DiscountToExamineTable.search();});
    checkDate = function () {DiscountToExamineTable.search();};
});

function auditPass() {//审核通过
    var id = $("#id").val();
    var discount = $("#discount").val();
    var discountGrade = $("#discountGrade").val();
    var oldStatus = $("#status").val();
    var oldAuditStatus = 0;
    var status = 0;
    if (oldStatus == "0") {
        if (discountGrade == 1) {
            status = 2;
            oldAuditStatus = 2;
        } else {
            status = 1;
            oldAuditStatus = 1;
        }
    } else if (oldStatus == "1") {
        status = 2;
        oldAuditStatus = 2;
    } else {
        layer.msg('该记录已经审核完成！', {time: 800, icon: 2});
        return false;
    }
    $.ajax({
        url: Feng.ctxPath + "/discountAudit/updateAuditStatus",
        dataType: "json",
        data: {
            "id": id,
            "status": status,
            "auditStatus": oldAuditStatus,
            "discount": discount
        },
        type: "POST",
        success: function (data) {
            if (data > 0) {
                layer.msg('操作成功！', {time: 1000, icon: 1});
                setTimeout(function () {
                    parent.location.reload();
                }, 2000);

            } else {
                layer.msg('审核失败！', {time: 1000, icon: 2});
                setTimeout(function () {
                    parent.location.reload();
                }, 2000);
            }
        }
    });
}

function auditFailed() {//审核不通过
    var id = $("#id").val();
    var status = $("#status").val();
    var discount = $("#discount").val();
    $.ajax({
        url: Feng.ctxPath + "/discountAudit/auditFailed",
        dataType: "json",
        data: {
            "id": id,
            "status": status,
            "auditStatus": "0",
            "discount": discount,
        },
        type: "POST",
        success: function (data) {
            if (data > 0) {
                layer.msg('操作成功！', {time: 800, icon: 1});
                setTimeout(function () {
                    parent.location.reload();
                }, 1000);

            } else {
                layer.msg('审核失败！', {time: 800, icon: 2});
                setTimeout(function () {
                    parent.location.reload();
                }, 1000);
            }
        }
    });
}

DiscountToExamineTable.approval = function (obj) {
    document.getElementById("auditStatusValue").value = obj;
    DiscountToExamineTable.search();
};